package cn.edu.klmy.controller;

import cn.edu.klmy.dao.IClaszDao;
import cn.edu.klmy.dao.IStudentClaszRelDao;
import cn.edu.klmy.dao.ITeacherClaszRelDao;
import cn.edu.klmy.dao.ITeacherDao;
import cn.edu.klmy.models.clasz.Clasz;
import cn.edu.klmy.models.teacher.Teacher;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import web.models.CommonResult;
import web.models.Page;
import web.util.RequestUtil;
import web.util.DaoUtil.SqlOperator;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/clasz")
public class ClaszController {

    @Resource(name = "claszDao")
    private IClaszDao claszDao;

    @Resource(name = "teacherDao")
    private ITeacherDao teacherDao;

    @Resource(name = "studentClaszRelDao")
    private IStudentClaszRelDao studentClaszRelDao;

    @Resource(name = "teacherClaszRelDao")
    private ITeacherClaszRelDao teacherClaszRelDao;

    @RequestMapping("/add")
    @ResponseBody
    public CommonResult addClasz(@ModelAttribute Clasz clasz) {
        claszDao.addClasz(clasz);
        return new CommonResult(new Boolean(true));
    }

    @RequestMapping("/add-and-instudents")
    @ResponseBody
    public CommonResult addClaszAndInStudents(@ModelAttribute Clasz clasz, @RequestParam String studentIdsStr) {
        claszDao.addClasz(clasz);
        studentClaszRelDao.addStudentsToClasz(RequestUtil.convertStrToIntArray(studentIdsStr), clasz.getId(), clasz.getType());
        return new CommonResult(new Boolean(true));
    }

    @RequestMapping("/update")
    @ResponseBody
    public CommonResult updateClasz(@ModelAttribute Clasz clasz, @RequestParam int oldType) {
    	// 删除班级中的学生
        studentClaszRelDao.removeAllStudentFromClasz(clasz.getId(), oldType);
        // 删除班级中的老师
        teacherClaszRelDao.removeAllTeacherFromClasz(clasz.getId());
        claszDao.updateClasz(clasz);
        return new CommonResult(new Boolean(true));
    }

    @RequestMapping("/exists")
    @ResponseBody
    public CommonResult claszExists(@RequestParam String claszName) {
        boolean result = claszDao.claszExists(claszName);
        return new CommonResult(result);
    }

    @RequestMapping("/remove")
    @ResponseBody
    public CommonResult removeClasz(@RequestParam int claszId, @RequestParam int claszType) {
    	// 删除班级中的学生
        studentClaszRelDao.removeAllStudentFromClasz(claszId, claszType);
        // 删除班级中的老师
        teacherClaszRelDao.removeAllTeacherFromClasz(claszId);
        // 删除班级与课程编排之间的关系。还未实现
        // 删除班级
        claszDao.removeClasz(claszId);
        return new CommonResult(new Boolean(true));
    }

    @RequestMapping("/{claszId}")
    @ResponseBody
    public Clasz getClasz(@PathVariable int claszId) {
        List<Clasz> result = claszDao.getClasz("id", claszId);
        if (null != result && !result.isEmpty())
            return result.get(0);
        return null;
    }

    @RequestMapping("/all")
    @ResponseBody
    public Page<Clasz> getClaszsPage(@RequestParam int page, @RequestParam int rows) {
        List<Clasz> claszs = claszDao.getClaszs(null, page, rows);
        int count = claszDao.getClaszCount(null);
        return new Page<Clasz>(count, claszs);
    }

    @RequestMapping("/{claszId}/all-teachers")
    @ResponseBody
    public List<Teacher> getTeachersInClasz(@PathVariable int claszId) {
        return teacherDao.getTeachersInClasz(claszId);
    }

    @RequestMapping("/not-in/{claszId}/teachers")
    @ResponseBody
    public List<Teacher> getTeachersNotInClasz(@PathVariable int claszId) {
        return teacherDao.getTeachersNotInClasz(claszId);
    }

    @RequestMapping("/not-in/{claszId}/teachers-byname")
    @ResponseBody
    public List<Teacher> getTeachersNotInClaszSearchByName(@PathVariable int claszId, @RequestParam String name) {
        List<Teacher> notOfTeachers = teacherDao.getTeachersNotInClasz(claszId);
        List<Teacher> result = new ArrayList<Teacher>();
        if (null != notOfTeachers) {
        	for (Teacher teacher : notOfTeachers) {
        		if (teacher.getName().contains(name)) {
        			result.add(teacher);
        		}
        	}
        }
        return result;
    }

    @RequestMapping("/all-admin-claszs")
    @ResponseBody
    public List<Clasz> getAllAdminClasz() {
        return claszDao.getClasz("class_type", 1);
    }

    @RequestMapping("/all-study-claszs")
    @ResponseBody
    public List<Clasz> getAllStudyClasz() {
        return claszDao.getClasz("class_type", 0);
    }

    @RequestMapping("/{claszId}/addstudents")
    @ResponseBody
    public CommonResult addStudentsToClasz(@PathVariable int claszId, @RequestParam int claszType, @RequestParam String studentIdsStr) {
        int[] studentIds = RequestUtil.convertStrToIntArray(studentIdsStr);
        studentClaszRelDao.addStudentsToClasz(studentIds, claszId, claszType);
        return new CommonResult(new Boolean(true));
    }

    @RequestMapping("/{claszId}/removestudents")
    @ResponseBody
    public CommonResult removeStudentsFromClasz(@PathVariable int claszId, @RequestParam int claszType, @RequestParam String studentIdsStr) {
        int[] studentIds = RequestUtil.convertStrToIntArray(studentIdsStr);
        studentClaszRelDao.removeStudentsFromClasz(studentIds, claszId, claszType);
        return new CommonResult(new Boolean(true));
    }

    @RequestMapping("/{claszId}/remove-all-students")
    @ResponseBody
    public CommonResult removeAllStudentFromClasz(@PathVariable int claszId) {
        studentClaszRelDao.removeStudentFromAllClasz(claszId);
        return new CommonResult(new Boolean(true));
    }

    @RequestMapping("/{claszId}/addteachers")
    @ResponseBody
    public CommonResult addTeachersToClasz(@PathVariable int claszId, @RequestParam String teacherIdsStr) {
        int[] teacherIds = RequestUtil.convertStrToIntArray(teacherIdsStr);
        teacherClaszRelDao.addTeachersToClasz(teacherIds, claszId);
        return new CommonResult(new Boolean(true));
    }

    @RequestMapping("/{claszId}/removeteachers")
    @ResponseBody
    public CommonResult removeTeachersFromClasz(@PathVariable int claszId, @RequestParam String teacherIdsStr) {
        int[] teacherIds = RequestUtil.convertStrToIntArray(teacherIdsStr);
        teacherClaszRelDao.removeTeachersFromClasz(teacherIds, claszId);
        return new CommonResult(new Boolean(true));
    }

    @RequestMapping("/clasz-by-name")
    @ResponseBody
    public Page<Clasz> getClaszsPage(@RequestParam String name, @RequestParam int page, @RequestParam int rows) {
    	return getClaszsByName(name, page, rows);
    }

    private Page<Clasz> getClaszsByName(String claszName, int page, int rows) {
        Map<String, Object[]> params = new HashMap<String, Object[]>();
        params.put("class_name", new Object[]{ SqlOperator.like, claszName });
        List<Clasz> claszs = claszDao.getClaszs(params, page, rows);
        int count = claszDao.getClaszCount(params);
        return new Page<Clasz>(count, claszs);
    }


    public void setClaszDao(IClaszDao claszDao) {
        this.claszDao = claszDao;
    }

    public void setStudentClaszRelDao(IStudentClaszRelDao studentClaszRelDao) {
        this.studentClaszRelDao = studentClaszRelDao;
    }

    public void setTeacherDao(ITeacherDao teacherDao) {
        this.teacherDao = teacherDao;
    }

    public void setTeacherClaszRelDao(ITeacherClaszRelDao teacherClaszRelDao) {
        this.teacherClaszRelDao = teacherClaszRelDao;
    }
}
